package com.yanh.seckill.controller;

import com.yanh.jcommon.utils.R;
import com.yanh.jcommon.vo.seckill.SeckillSessionWithSku;
import com.yanh.seckill.service.SeckillService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Tag(name="秒杀接口",description = "秒杀接口")
@RestController
@RequestMapping("seckill/Seckill")
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 秒杀活动列表
     * @return
     */
    @Operation(summary = "秒杀活动列表",description = "秒杀活动列表")
    @GetMapping(value = "/getSeckillSessionSkuList")
    @ResponseBody
    public R getSeckillList() {
        List<SeckillSessionWithSku> vos = seckillService.getSeckillSessionSkuList();
        return R.ok().setData(vos);
    }

    /**
     * 根据skuId查询商品秒杀活动信息
     * @param skuId
     * @return
     */
    @Operation(summary = "根据skuId查询商品秒杀活动信息",description = "根据skuId查询商品秒杀活动信息")
    @GetMapping(value = "/sku/{skuId}")
    @ResponseBody
    public R getSkuSeckilInfo(@PathVariable("skuId") Long skuId) {

        var to = seckillService.getSkuSeckillSessionInfo(skuId);
        return R.ok().setData(to);
    }

    /**
     * 商品秒杀
     * @param promotionId
     * @param skuId
     * @param randomCode
     * @param num
     * @return
     */
    @Operation(summary = "商品秒杀",description = "商品秒杀")
    @GetMapping(value = "/kill")
    public String seckill(
                          @RequestParam("promotionId") Long promotionId,
                          @RequestParam("skuId") Long skuId,
                          @RequestParam("randomCode") String randomCode,
                          @RequestParam("num") Integer num) {
        String orderSn = "秒杀失败";
        try {
            orderSn = seckillService.kill(promotionId,skuId,randomCode,num);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orderSn;
    }

}
